package org.person.org.service.impl;


import org.person.basic.service.impl.BaseServiceImpl;
import org.person.org.domain.Department;
import org.person.org.mapper.DepartmentMapper;
import org.person.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
@Service
public class DepartmentServiceImplImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Transactional
    @Override
    public void insert(Department department) {
        departmentMapper.insert(department);
        if (Objects.isNull(department.getParent().getId())){
            department.setPath("/"+department.getId());
        }else {
            Department department1 = departmentMapper.queryById(department.getParent().getId());
            department.setPath(department1.getPath() + "/" + department.getId());
        }
        departmentMapper.update(department);
    }
    @Transactional
    @Override
    public void update(Department department) {

        if (Objects.isNull(department.getParent().getId())){
            // 修改为一级部门
            department.setPath("/" + department.getId());
        }else{
            Department department1 = departmentMapper.queryById(department.getParent().getId());
            department.setPath(department1.getPath() + "/" + department.getId());
        }
        departmentMapper.update(department);
    }
    @Override
    public List<Department> getTree() {
        return departmentMapper.getTree();
    }
}
